package fr.mywiki.model.ejbAdapter.services;

import javax.ejb.CreateException;
import javax.ejb.FinderException;
import javax.ejb.SessionBean;

import fr.mywiki.business.account.AccountLite;
import fr.mywiki.business.entry.EntryLite;
import fr.mywiki.business.structure.StructureLite;
import fr.mywiki.business.versioning.VersionLite;
import fr.mywiki.model.ejbAdapter.EntityHomeCache;
import fr.mywiki.model.ejbAdapter.ServicesAdapter;
import fr.mywiki.model.ejbAdapter.entity.AccountLocal;
import fr.mywiki.model.ejbAdapter.entity.EntryLocal;
import fr.mywiki.model.ejbAdapter.entity.StructureLocal;
import fr.mywiki.model.ejbAdapter.entity.VersionLocal;
import fr.mywiki.model.ejbAdapter.services.business.AddLinkServiceBusiness;

/**
 * This session bean deals with adding links to bean objects.
 * 
 * @ejb.bean name = "AddLinkService" 
 * 		display-name = "AddLinkService"
 * 		description = "AddLink Administration Service"
 * 		jndi-name="ejb/MyWiki/AddLinkService"
 * 		remote-business-interface = "fr.mywiki.model.ejbAdapter.services.business.AddLinkServiceBusiness"
 * 
 * @ejb.home
 * 		extends = "javax.ejb.EJBHome"
 * 		package = "fr.mywiki.model.ejbAdapter.services"
 *  
 * @ejb.interface
 * 		extends = "javax.ejb.EJBObject"
 * 		package = "fr.mywiki.model.ejbAdapter.services" 
 *   
 * @author tlombard
 */
public class AddLinkServiceBean extends ServicesAdapter implements AddLinkServiceBusiness,SessionBean {
	/**
	 * Default create method
	 * @throws CreateException
	 * @ejb.create-method 
	 */
	public void ejbCreate() throws CreateException {
	}
	
	/**
	 * The serial version UID.
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * Links the <code>AccountLite</code> <tt>account</tt> argument to the
	 * <code>Value</code> <tt>added</tt> via the relation <tt>relName</tt>.
	 * 
	 * @param accountId a <code>Long</code> object, the identifier of the <i>from</i> object.
	 * @param addedId a <code>Long</code> object, the id of a Value of the correct type, the <i>to</i> object.
	 * @param relName the considered relation.
	 * 
	 * @ejb:interface-method view-type="local"
	 */
	public void addLinkAccount(Long accountId, Long addedId, String relName)
			throws FinderException {
		AccountLocal bean = EntityHomeCache.getInstance().getAccountHome()
				.findByPrimaryKey(accountId);
		if (AccountLite.LINK_ACCOUNT_STRUCTURE.equals(relName)) {
			StructureLocal structure = EntityHomeCache.getInstance()
					.getStructureHome().findByPrimaryKey(addedId);
			bean.setStructure(structure);
		}
	}
	
	/**
	 * Links the <code>EntryLite</code> <tt>entry</tt> argument to the
	 * <code>Value</code> <tt>added</tt> via the relation <tt>relName</tt>.
	 * 
	 * @param entryId a <code>Long</code> object, the identifier of the <i>from</i> object.
	 * @param addedId a <code>Long</code> object, the id of a Value of the correct type, the <i>to</i> object.
	 * @param relName the considered relation.
	 * 
	 * @ejb:interface-method view-type="local"
	 */
	public void addLinkEntry(Long entryId, Long addedId, String relName)
			throws FinderException {
		EntryLocal bean = EntityHomeCache.getInstance().getEntryHome()
				.findByPrimaryKey(entryId);
		if (EntryLite.LINK_VERSION_ENTRY.equals(relName)) {
      VersionLocal version = EntityHomeCache.getInstance().getVersionHome()
          .findByPrimaryKey(addedId);
      bean.getVersions().add(version);
    }
  }
	
	/**
	 * Links the <code>StructureLite</code> <tt>structure</tt> argument to the
	 * <code>Value</code> <tt>added</tt> via the relation <tt>relName</tt>.
	 * 
	 * @param structureId a <code>Long</code> object, the identifier of the <i>from</i> object.
	 * @param addedId a <code>Long</code> object, the id of a Value of the correct type, the <i>to</i> object.
	 * @param relName the considered relation.
	 * 
	 * @ejb:interface-method view-type="local"
	 */
	public void addLinkStructure(Long structureId, Long addedId, String relName)
			throws FinderException {
		StructureLocal bean = EntityHomeCache.getInstance().getStructureHome()
				.findByPrimaryKey(structureId);
		if (StructureLite.LINK_PARENT_TO_CHILDREN.equals(relName)) {
			StructureLocal structure = EntityHomeCache.getInstance()
					.getStructureHome().findByPrimaryKey(addedId);
			bean.getChildren().add(structure);
		} else if (StructureLite.LINK_CHILD_TO_PARENT.equals(relName)) {
			StructureLocal structure = EntityHomeCache.getInstance()
					.getStructureHome().findByPrimaryKey(addedId);
			bean.setParent(structure);
		} else if (StructureLite.LINK_ACCOUNT_STRUCTURE.equals(relName)) {
			AccountLocal account = EntityHomeCache.getInstance().getAccountHome()
					.findByPrimaryKey(addedId);
			bean.getAccounts().add(account);
		}
	}
	
	/**
	 * Links the <code>VersionLite</code> <tt>version</tt> argument to the
	 * <code>Value</code> <tt>added</tt> via the relation <tt>relName</tt>.
	 * 
	 * @param versionId
	 *          a <code>Long</code> object, the identifier of the <i>from</i>
	 *          object.
	 * @param addedId
	 *          a <code>Long</code> object, the id of a Value of the correct
	 *          type, the <i>to</i> object.
	 * @param relName
	 *          the considered relation.
	 * 
	 * @ejb:interface-method view-type="local"
	 */
	public void addLinkVersion(Long versionId, Long addedId, String relName)
			throws FinderException {
		VersionLocal bean = EntityHomeCache.getInstance().getVersionHome()
				.findByPrimaryKey(versionId);
		if (VersionLite.LINK_VERSION_ENTRY.equals(relName)) {
      EntryLocal entry = EntityHomeCache.getInstance().getEntryHome()
          .findByPrimaryKey(addedId);
      bean.setEntry(entry);
    }
	}
}
